package com.transsion.clouddemo

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.transsion.clouddemo.CloudApplication.Companion.initCloudSdk
import com.transsion.clouddemo.core.CloudHomeActivity
import com.transsion.clouddemo.core.Custom.TransIDManager
import com.transsion.clouddemo.core.Custom.utils.SPUtils
import com.transsion.clouddemo.core.config.ConfigFragment.Companion.KEY_ENV
import com.transsion.palmsdk.PalmID
import com.transsion.widgetslib.dialog.PromptDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

    var mTimeDialog: PromptDialog ?= null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        if (intent?.getBooleanExtra("isInit", false) == true) {
            //重新初始化账号SDK
            TransIDManager.init(if (SPUtils.getString(KEY_ENV).equals("test")) PalmID.SERVER_TEST else PalmID.SERVER_ONLINE)
            //初始化云服务SDK
            initCloudSdk()
        }

        // 登录账号
        lifecycleScope.launch {
            delay(1000)


            try {
                if (!TransIDManager.isLoggingIn) {
                    val authorize = TransIDManager.authorize(this@MainActivity)
//                TransIDManager.loginByToken()

//                    Log.e("zhangxin", "authorize: $authorize")
//                    Log.e("zhangxin", "userInfo: ${TransIDManager.userBean}")

                    if (authorize.isNotEmpty() ) {
                        val intent =
                            Intent(this@MainActivity, CloudHomeActivity::class.java).apply {
                                flags =
                                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                            }
                        startActivity(intent)
                        finish() // 销毁当前的登录页
                    }
                }
            } catch (exception: Exception) {
                Log.e("zhangxin", exception.message!!)
                showErrorDialog()
            }


        }
    }

     fun showErrorDialog() {
        mTimeDialog = PromptDialog.Builder(this)
            .setTitle(R.string.app_name)
            .setMessage(R.string.cloud_login_message)
            .setPositiveButton(R.string.cloud_login_positive) { _, _ ->
                restartActivity()
            }
            .setCanceledOnTouchOutside(false)
            .setNegativeButton(R.string.cloud_login_cancel) { _, _ ->
                finish()
            }
            .show()
    }

    private fun restartActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        mTimeDialog?.dismiss() // 确保对话框在 Activity 销毁时被解除
    }


}
